﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Tesla.Math;

namespace Tesla.Bounding {
    /// <summary>
    /// Represents the results from a ray-object intersection test. This structure simply holds the results of
    /// a test, but does not provide the test itself.
    /// </summary>
    public struct PickResult {
        private Ray _ray;
        private IPickable _target;
        private BoundingIntersectionRecord? _boundRecord;
        private PrimitiveIntersectionRecord? _primRecord;

        /// <summary>
        /// Gets the ray used in the intersection testing.
        /// </summary>
        public Ray SourceRay {
            get {
                return _ray;
            }
        }

        /// <summary>
        /// Gets the target object.
        /// </summary>
        public IPickable Target {
            get {
                return _target;
            }
        }

        /// <summary>
        /// Gets the bounding intersection record, if it exists. If it does not,
        /// then the result means the ray does not intersect the pickable's bounding volume or
        /// the test was not conducted.
        /// </summary>
        public BoundingIntersectionRecord? BoundingIntersectionRecord {
            get {
                return _boundRecord;
            }
        }

        /// <summary>
        /// Gets the primitive intersection record, if it exists. If it does not,
        /// then the result may have multiple meanings: the ray does not intersect at all, or it
        /// intersects the pickable's bounding volume (which is a false-positive), or lastly,
        /// the primitive intersection was not checked.
        /// </summary>
        public PrimitiveIntersectionRecord? PrimitiveIntersectionRecord {
            get {
                return _primRecord;
            }
        }

        /// <summary>
        /// Creates a new PickResult.
        /// </summary>
        /// <param name="ray">Ray used in the test</param>
        /// <param name="target">Target object</param>
        /// <param name="boundingIntersection">The bounding intersection record.</param>
        public PickResult(Ray ray, IPickable target, BoundingIntersectionRecord boundingIntersection) {
            _ray = ray;
            _target = target;
            _boundRecord = boundingIntersection;
            _primRecord = null;
        }

        /// <summary>
        /// Creates a new PickResult.
        /// </summary>
        /// <param name="ray">Ray used in the test</param>
        /// <param name="target">Target object</param>
        /// <param name="primIntersection">The primitive intersection record.</param>
        public PickResult(Ray ray, IPickable target, PrimitiveIntersectionRecord primIntersection) {
            _ray = ray;
            _target = target;
            _boundRecord = null;
            _primRecord = primIntersection;
        }

        /// <summary>
        /// Creates a new PickResult.
        /// </summary>
        /// <param name="ray">Ray used in the test</param>
        /// <param name="target">Target object</param>
        /// <param name="boundingIntersection">The bounding intersection record.</param>
        /// <param name="primIntersection">The primitive intersection record.</param>
        public PickResult(Ray ray, IPickable target, BoundingIntersectionRecord boundingIntersection, PrimitiveIntersectionRecord primIntersection) {
            _ray = ray;
            _target = target;
            _boundRecord = boundingIntersection;
            _primRecord = primIntersection;
        }
    }
}
